package logic;

import java.io.IOException;
import java.util.GregorianCalendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import data.Persona;

@WebServlet("/CreaPersonaServlet")
public class CreaPersonaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public CreaPersonaServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Persona p = new Persona();
		p.setCognome(request.getParameter("cognome"));
		p.setNome(request.getParameter("nome"));
		String dataDiNascitaS = request.getParameter("dataDiNascita");
		GregorianCalendar dataDiNascitaGC = new GregorianCalendar(
				Integer.parseInt(dataDiNascitaS.split("/")[2]),
				Integer.parseInt(dataDiNascitaS.split("/")[1]),
				Integer.parseInt(dataDiNascitaS.split("/")[0]));
		p.setDataDiNascita(dataDiNascitaGC);
		p.setSesso(request.getParameter("sesso").charAt(0));
		p.setComuneDiNascita(request.getParameter("comuneDiNascita"));
		
		String codiceFiscale = p.getCodiceFiscale();
		HttpSession session = request.getSession();
		session.setAttribute("codiceFiscale", codiceFiscale);
		response.sendRedirect("Index.jsp");
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
